<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetOrder.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\order;

use app\api\model\dao\DuoBao;
use app\api\model\dao\Goods;
use app\api\model\dao\IssuesNumber;
use app\api\model\dao\Lucky;
use app\api\model\dao\OrderGoods;
use app\api\model\dao\OrderInfo;
use app\api\model\dao\UserAccount;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;
use app\library\Flag;

class GetDuoBaoOrder extends PageBase
{

    private $objOrderInfo;
    private $objOrderGoods;
    private $objGoods;
    private $objDuoBao;
    private $objIssuesNumber;
    private $objLucky;
    private $objUserAccount;

    public function __construct()
    {
        parent::__construct();
        $this->objOrderInfo = new OrderInfo();
        $this->objOrderGoods = new OrderGoods();
        $this->objGoods = new Goods();
        $this->objDuoBao = new DuoBao();
        $this->objIssuesNumber = new IssuesNumber();
        $this->objLucky = new Lucky();
        $this->objUserAccount = new UserAccount();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'status' => 'require',
        ];
        $msg = [
            'status.require' => '订单状态必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // TODO:业务逻辑
        // 1、参数校验
        //$ret = $this->checkParam($param);
        //if ($ret) {
        //    return $this->getRes(Error::ERR_PARAM, $ret);
        //}

        // 获取订单信息
        $arrCond = array();
        $arrCond['user_id'] = $this->user_id;
        $arrCond['order_status'] = ['<>', Flag::ORDER_STATUS_DELETE];

        if (!empty($param['status'])) {
            if ($param['status'] == 11) {
                $arrCond['order_status'] = ['=', Flag::ORDER_STATUS_WKJ];
            }

            if ($param['status'] == 12) {
                $arrCond['order_status'] = ['=', Flag::ORDER_STATUS_YKJ];
            }

            if ($param['status'] == 13) {
                $arrCond['order_status'] = ['=', Flag::ORDER_STATUS_YZJ];
            }
        }

        $arrCond['order_type'] = ['=', Flag::ORDER_TYPE_YIYUANDUOBAO];

        $perpage = isset($param['perpage']) ? intval($param['perpage']) : 0;
        $page = isset($param['page']) ? intval($param['page']) : 0;
        $perpage = max(10, $perpage);
        $page = max(1, $page);
        $offset = ($page - 1) * $perpage;
        $limit = $perpage;

        $counts = $this->objOrderInfo->getCount($arrCond);
        $orderInfos = $this->objOrderInfo->getOrderByCond($arrCond, [], $offset, $limit, ' add_time desc ');
        if (false === $orderInfos) {
            return $this->getRes(Error::ERR_SYS);
        }

        foreach ($orderInfos as &$orderInfo) {

            // 获取商品
            $goods_ids = [];
            $goods_arr = [];

            // 获取商品信息
            $orderGoods = $this->objOrderGoods->getOrderGoods($orderInfo['order_id']);
            $goods_ids = array_map(function ($v) {
                return $v['goods_id'];
            }, $orderGoods);
            $goods = $this->objGoods->getGoods(implode(',', $goods_ids));
            foreach ($goods as $good) {
                $goods_arr[$good['goods_id']] = $good;
            }

            foreach ($orderGoods as &$v) {
                if (isset($goods_arr[$v['goods_id']])) {
                    $v['market_price'] = $goods_arr[$v['goods_id']]['market_price'];
                    $v['shop_price'] = $goods_arr[$v['goods_id']]['shop_price'];
                    $v['sale_count'] = $goods_arr[$v['goods_id']]['sale_count'];
                    // 加上折扣
                    $v['discount'] = '' . round($goods_arr[$v['goods_id']]['shop_price'] / $goods_arr[$v['goods_id']]['market_price'], 1) * 10;
                    $v['goods_thumb'] = $goods_arr[$v['goods_id']]['goods_thumb'];
                }
            }
            $orderInfo['goods'] = $orderGoods;

            $issues_no = $orderInfo['issues_no'];
            $arrCond = array();
            $arrCond['issues_no'] = ['=', $issues_no];
            $issues = $this->objIssuesNumber->getIssuesNumberByCond($arrCond, []);
            $orderInfo['issues'] = $issues;

            $arrCond = array();
            $arrCond['issues_no'] = ['=', $issues_no];
            $arrCond['user_id'] = ['=', $this->user_id];
            $duobao = $this->objDuoBao->getDuoBaoByCond($arrCond, []);
            $orderInfo['duobao'] = $duobao;

            $arrCond = array();
            $arrCond['issues_no'] = ['=', $issues_no];
            $lucky = $this->objLucky->getLuckyByCond($arrCond, []);

            if (!empty($lucky)) {
                $lucky = $lucky[0];
                $arrConds = array();
                $arrCond['id'] = ['=', $lucky['user_id']];
                $arrFields = ['id', 'user_name'];
                $users = $this->objUserAccount->getUserAccountByConds($arrConds, $arrFields);

                $i = 1;
                $orderInfo['lucky'] = array();
                $orderInfo['lucky']['no'] = "第" . $i . "期";
                $orderInfo['lucky']['reward_id'] = $lucky['user_id'];
                $orderInfo['lucky']['reward_name'] = $users[0]['user_name'];
                $orderInfo['lucky']['buy_volume'] = $lucky['issues_no'];;
                $orderInfo['lucky']['open_time'] = date('Y年m月d日 H时i分s秒', $lucky['create_time']);
                $orderInfo['lucky']['reward_no'] = $lucky['db_no'];
            }


        }


        $this->data = array(
            'list' => $orderInfos,
            'total' => $counts,
        );

        return $this->getRes();
    }
}
